package com.zero.mms.controller;

import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.basic.model.type.PositionType;
import com.zero.basic.util.JsonCreator;
import com.zero.mms.model.Position;
import com.zero.mms.model.UserAccount;
import com.zero.youxiao.dao.UserAccountDao;
import com.zero.youxiao.dto.MenuDto;
import com.zero.youxiao.service.IndexService;
import com.zero.youxiao.service.UserService;

@Controller("userAccountController")
@RequestMapping("userAccount")
public class UserAccountController {

	@Inject
	private UserService userService;
	
	@Inject
	private IndexService indexService;

	/*
	@RequestMapping("/regist")
	@ResponseBody
	public Object regist() {
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			userService.registProc();

			objectNode.put("response", "");
			objectNode.put("message", "注册用户成功");
			objectNode.put("statusCode",SmsStatusCode.SUC_CODE);
		} catch(SmsRuntimeException e){
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error",e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		}catch (Exception e) {
			objectNode.put("message",SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error",SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode",SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}
	*/
	
	@RequestMapping("/login")
	public String regist(String userName,String passWord,String checkCode,HttpServletRequest request,HttpServletResponse response,HttpSession httpSession) {
		try {
			System.out.println(userName);
			if(!checkCode.equals(httpSession.getAttribute("cc"))){
				request.setAttribute("checkCodeMsg", "验证码错误");
				return "index";
			}
			UserAccount loginAccount = new UserAccount();
			loginAccount.setUserName(userName);
			loginAccount.setUserPass(passWord);
			userService.login(loginAccount);

			System.out.println("login suc");
			
			request.setAttribute("loginSuc", "true");
			return "index";
		} catch(SmsRuntimeException e){
			System.out.println(e.getErrorDescription());
			if(e.getError().equals(SmsErrorConstant.AUTH_USER_NOT_EXISTS))
				request.setAttribute("userMsg", e.getErrorDescription());
			else if(e.getError().equals(SmsErrorConstant.AUTH_BAD_ACCESS))
				request.setAttribute("passMsg", e.getErrorDescription());
			return "index";
		}catch (Exception e) {
			return "index";
		}
	}
	
	@RequestMapping("/menus")
	@ResponseBody
	public Object getMenus(HttpServletResponse response, HttpSession session) {
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			List<MenuDto> menuDtos = indexService.getAllMenus();
			objectNode.putPOJO("response", menuDtos);
			objectNode.put("message", "注册用户成功");
			objectNode.put("statusCode",SmsStatusCode.SUC_CODE);
		} catch(SmsRuntimeException e){
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error",e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		}catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message",SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error",SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode",SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}
	
}
